﻿Imports System.IO
Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class HttpServer

    Public StateText As Label

    Public State As Integer
    Public Msg As String
    Public DataSource As String

    Public DocLists As List(Of PrintDoc)

    Private Function RequestApi(WM_ACTION As String)
        Dim WM_SERVER As String = My.Settings.WM_SERVER
        Dim WM_APPID As String = My.Settings.WM_APPID
        Dim WM_TOKEN As String = My.Settings.WM_TOKEN

        Dim WM_TIMESTAMP As String = Timer_Stamp()
        Dim Token As String = Me.GetToken(WM_APPID, WM_TOKEN, WM_TIMESTAMP)
        Dim API = WM_SERVER + "/" + WM_ACTION + "?appid=" + WM_APPID + "&token=" + Token + "&timestamp=" + WM_TIMESTAMP
        'Console.WriteLine(API)
        Return API
    End Function

    Public Function RequestData()
        Dim API = RequestApi("datasource")
        Dim Request As HttpWebRequest = WebRequest.Create(API)
        Request.Method = "GET"

        Dim sr As StreamReader = New StreamReader(Request.GetResponse().GetResponseStream)
        Dim jres As JObject = CType(JsonConvert.DeserializeObject(sr.ReadToEnd), JObject)

        If jres IsNot Nothing Then
            Dim res_state As Boolean = jres("state").ToObject(Of Boolean)
            Dim res_msg As String = jres("msg").ToString

            If res_state <> True Then
                Msg = res_msg
                Return False
                'Me.Invoke_Thread(res_msg)
            Else
                'Console.WriteLine(jres("data").ToString)
                DataSource = jres("data").ToString
                DocLists = JsonConvert.DeserializeObject(Of List(Of PrintDoc))(jres("data").ToString)
                Dim PrinterDrawing As New PrinterDrawing With {
                    .DocList = DocLists
                }

                Dim pdocCount As Integer = DocLists.Count
                Msg = "获取" + pdocCount.ToString + "条数据"
                'Me.Invoke_Thread("获取" + pdocCount.ToString + "条数据")
                If pdocCount > 0 Then
                    Return True
                End If

            End If
            Return False
        Else
            Msg = "远程数据解析失败"
            'Me.Invoke_Thread("远程数据解析失败")
            Return False
        End If
    End Function

    Public Function TestConnect()

        Dim API = RequestApi("hello")
        Try
            StateText.Content = "发起请求"
            Dim request As HttpWebRequest = WebRequest.Create(API)
            request.Method = "GET"
            Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)
            Dim jres As JObject = CType(JsonConvert.DeserializeObject(sr.ReadToEnd), JObject)

            If jres IsNot Nothing Then
                Dim res_state As Boolean = jres("state").ToObject(Of Boolean)
                Dim res_msg As String = jres("msg").ToString

                StateText.Content = res_msg
                Return res_state
            Else
                StateText.Content = "远程数据解析失败"
                Return False
            End If
        Catch ex As Exception
            StateText.Content = ex.Message
            Return False
        End Try

    End Function

    Private Function GetToken(WM_APPID As String, WM_TOKEN As String, WM_TIMESTAMP As String)
        Dim RToken As String = ""
        Dim Token As String = WM_APPID + WM_TOKEN + WM_TIMESTAMP
        Dim SHA As New System.Security.Cryptography.SHA1CryptoServiceProvider 'sha1加密

        Dim bytValue() As Byte '输入
        Dim bytHash() As Byte '输出
        bytValue = System.Text.Encoding.ASCII.GetBytes(Token) '输入字符转为byte
        bytHash = SHA.ComputeHash(bytValue) '加密

        For Each b As Byte In bytHash
            RToken += b.ToString("x2")
        Next
        Return RToken
    End Function

    Function Timer_Stamp() As String
        Dim dtStart As DateTime = TimeZone.CurrentTimeZone.ToLocalTime(New DateTime(1970, 1, 1))
        Dim dtNow As DateTime = DateTime.Parse(DateTime.Now.ToString())
        Dim toNow As TimeSpan = dtNow.Subtract(dtStart)
        Dim timeStamp As String = toNow.Ticks.ToString()
        timeStamp = timeStamp.Substring(0, timeStamp.Length - 7)
        Return timeStamp
    End Function


End Class
